package cn.demo.d01;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Demo05 {
    /**
     模拟双色球中奖号码
     * */
    public static void main(String[] args) {
        int[] drawnNums = genDrawnNumbers();
        System.out.println("中奖号码: "+Arrays.toString(drawnNums));
        int[] userNums = enterUserNumber();
        System.out.println(Arrays.toString(userNums));
        lottery(drawnNums,userNums);
    }

    // 开奖
    private static void lottery(int[] drawnNums,int[] userNums) {
        int redCount = 0;
        int blueCount = 0;
        // 判断红球
        for (int i = 0; i < userNums.length-1; i++) {
            for (int j = 0; j < drawnNums.length-1; j++) {
                if(userNums[i] == drawnNums[j]){
                    redCount++;
                    break;
                }
            }
        }
        // 判断蓝球
        if (userNums[userNums.length-1] == drawnNums[drawnNums.length-1]){
            blueCount++;
        }
        //根据红球的个数以及蓝球的个数来判断中奖情况
        if(redCount == 6 && blueCount == 1){
            System.out.println("恭喜你，中奖1000万");
        }else if(redCount == 6 && blueCount == 0){
            System.out.println("恭喜你，中奖500万");
        }else if(redCount == 5 && blueCount == 1){
            System.out.println("恭喜你，中奖3000");
        }else if((redCount == 5 && blueCount == 0) ||  (redCount == 4 && blueCount == 1)){
            System.out.println("恭喜你，中奖200");
        }else if((redCount == 4 && blueCount == 0) ||  (redCount == 3 && blueCount == 1)){
            System.out.println("恭喜你，中奖10");
        }else if((redCount == 2 && blueCount == 1) ||  (redCount == 1 && blueCount == 1)|| (redCount == 0 && blueCount == 1)){
            System.out.println("恭喜你，中奖5");
        }else{
            System.out.println("谢谢参与，谢谢惠顾");
        }
        System.out.println("红球中奖"+redCount+"个,蓝球中奖"+blueCount+"个");
        System.out.println("中奖号码: "+Arrays.toString(drawnNums));
        System.out.println("你的号码: "+Arrays.toString(userNums));
    }

    // 录入用户选择号码
    private static int[] enterUserNumber() {
        int[] userNums = new int[7];
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入6个红球");
        // 录入红球
        for (int i = 0; i < userNums.length-1; ) {
            System.out.println("请输入第"+(i+1)+"个红球号码");
            int redNum = scanner.nextInt();
            if (!(redNum>=1&&redNum<=33)){
                System.out.println(redNum+"号码超出红球号码1-33范围");
            }else if(contains(userNums,redNum)){
                System.out.println(redNum+"号码已经存在");
            }else{
                userNums[i] = redNum;
                i++;
            }
        }
        // 录入篮球
        System.out.println("请输入篮球号码");
        while (true){
            int blueNum = scanner.nextInt();
            if (blueNum>=1&&blueNum<=16){
                userNums[userNums.length-1] = blueNum;
                break;
            }
            System.out.println("篮球号码范围是1-16,请确认");
        }

        return userNums;
    }

    // 生成中奖号码
    private static int[] genDrawnNumbers() {
        int[] drawnNums = new int[7];
        Scanner scanner = new Scanner(System.in);
        Random r = new Random();
        // 生成红球
        for (int i = 0; i < drawnNums.length-1; ) {
            int redNum = r.nextInt(32)+1;
            if (!contains(drawnNums,redNum)){
                drawnNums[i] = redNum;
                i++;
            }
        }
        // 录入篮球
        drawnNums[drawnNums.length-1] = new Random().nextInt(16)+1;
        return drawnNums;
    }

    private static boolean contains(int[] arr,int num){
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            if (num == arr[i]){
                flag = true;
                break;
            }
        }
        return flag;
    }
}
